<?php
/**
 * 
 * 
 * $Rev: 6 $
 * $Author: thejahweh $
 * $Date: 2010-08-04 15:55:34 +0200 (Mi, 04 Aug 2010) $
 * $Id: plugin.class.php 6 2010-08-04 13:55:34Z thejahweh $
 * $URL: https://allone.googlecode.com/svn/trunk/plugins/core/plugin.class.php $
 * @author: Adrian Liechti <info@jahweh.ch>
 * @copyright: 2010 Adrian Liechti, GPL3 and CC3 BY-SA
 */

class plugin {
	
	protected	$cfg;
	protected	$pluginsDir;
	protected	$id;
	protected	$dir;
	protected	$xmlPath;
	protected	$xml;
	protected	$isLoaded = false;
	protected	$priority = 4;
	protected	$isCached = false;
	protected	$register;
	protected	$requires;
	
	public function __construct ($id, $pluginsDir, $xml = false) {
		$this->id				= $id;
		$this->pluginsDir		= $pluginsDir;
		$this->dir				= $this->pluginsDir.'/'.$id;
		
		u::o('load: '.$id.' (<span class="path">'.$this->dir.'</span>)', 'plugin', 0);
		if (is_dir($this->dir)) {
			if ($xml !== false and get_class($xml) === 'SimpleXMLElement') {
				$this->isCached = true;
			} elseif ($this->xmlPath = $id.'.xml' and file_exists($this->dir.'/'.$this->xmlPath)) {
				$xml = simplexml_load_file($this->dir.'/'.$this->xmlPath);
			} elseif ($this->xmlPath = $id.'.xml.php' and file_exists($this->dir.'/'.$this->xmlPath)) {
				$xml = u::loadInvisibleSimpleXMLFile($this->dir.'/'.$this->xmlPath);
			} else {
				return $this->isLoaded = false;
			}
			return $this->isLoaded = $this->getByXML($xml);
		}
		return $this->isLoaded = false;
	}
	
	protected function getByXML (SimpleXMLElement &$xml) {
		if ($xml) {
			$this->xml =& $xml;
			$this->xml['id'] = $this->id;
			if ($this->isCached === true) {
				$this->xmlPath			= $this->xml['xmlPath'];
			} else {
				$this->xml['xmlPath']	= $this->xmlPath;
			}
			u::o('xmlPath: <span class="path">'.$this->xmlPath.'</span>', 'plugin', 1);
			if ($this->xml['priority'] === NULL) {
				$this->xml['priority']	= $this->priority;
			} else {
				$this->priority			= $this->xml['priority'];
			}
			/**
			 * TODO Relativer Pfad für 'pluginsDir' herausfiltern!
			 */
			if ($this->xml['pluginsDir'] === NULL) {
				$this->xml['pluginsDir']= $this->pluginsDir;
			}
			$this->register =& $this->xml->register;
			$this->requires =& $this->xml->requires;
			u::o('priority: '.$this->priority, 'plugin', 1);
			return $this->isLoaded = true;
		} else {
			/**
			 * TODO XML Fehler abfangen!
			 */
			return false;
		}
	}
	
	public function registerInclude () {
		global $pluginLoader, $t;
		if (isset($this->register->include)) {
			$t = $this;
			foreach ($this->register->include as $inc) {
				$fn = $this->dir.'/'.$inc['path'];
				/**
				 * TODO Geschwindigkeitsoptimierung
				 */
				if (file_exists($fn)/* and is_file($fn)*/) {
					u::o('<span class="path">'.$inc['path'].'</span>', 'plugin:registerInclude', 2);
					include_once($fn);
				} else {
					u::o('failed(<span class="path">'.$inc['path'].'</span>)', 'plugin:registerInclude', 2);
				}
			}
			unset($t);
			return true;
		}
		return false;
	}
	
	public function &getRegistred($groupName, $entryName = false) {
		global $pluginLoader;
		/**
		 * TODO Caching, beschleunigung...
		 * TODO Testen, ob es mit den Key's und den Child's funktioniert
		 */
		if (array_key_exists('plugins', $pluginLoader->register) and $reg =& $pluginLoader->register->plugins
			and array_key_exists($this->id, $reg->plugins) and $plugin =& $reg->$this->id)
		{
			if ($entryName === false) {
				$children = $plugin->$this->id->children($groupName);
				if (count($children) > 0) {
					return $children;
				}
			} elseif (	array_key_exists($groupName, $plugin->$this->id)
				and		($children = $plugin->$this->id->$groupName->children($groupName))
				and		count($children) > 0)
			{
				return $children;
			}
		}
		return false;
	}
	
	public function __get ($name) {
		return $this->$name;
	}
	
	public function __set ($name, $value) {
		if ($name === 'cfg' and $this->cfg === NULL) {
			$this->cfg = $value;
		}
	}
	
}
?>